package com.study.zkyxgs1.exception;

import com.study.zkyxgs1.utils.ResultMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理类
 * @author weixiaochun
 * @date 2022/6/14 21:51
 */
@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(ZkyxgsException.class)
    public ResultMap error(ZkyxgsException e){
        log.error(e.getMsg());
        e.printStackTrace();
        return ResultMap.error().code(e.getCode()).message(e.getMsg());
    }

    /**
     * 处理全部异常
     * @param e
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public ResultMap error(Exception e){
        e.printStackTrace();
        return ResultMap.error("服务器繁忙，请稍后再试...");
    }
}
